// by Xeno
//#define __DEBUG__
#define THIS_FILE "x_jip.sqf"
#include "x_setup.sqf"
if (!X_Client || {!isNil QGVAR(jip_started)}) exitWith {};
GVAR(jip_started) = true;

GVAR(lastovercastchangetime) = -1;

if (GVAR(weather) == 0 && {GVAR(FastTime) == 0}) then {
	if (GVAR(with_fog) == 0) then {
		if (!isNil QGVAR(fog)) then {
			GVAR(lastfog) = GVAR(fog) select 0;
			_ntt = ((GVAR(fog) select 1) - time) max 0;
			_ntt setFog GVAR(lastfog);
		} else {
			GVAR(lastfog) = [0,0.1,0];
		};
	};
	
	if (!isNil QGVAR(overcast)) then {
		GVAR(lastovercast) = GVAR(overcast) select 0;
		#ifdef __DEBUG__
		_date = date;
		__TRACE_1("","_date");
		#endif
		skipTime -24;
		86400 setOvercast GVAR(lastovercast);
		skipTime 24;
		0 spawn {sleep 0.1; simulweathersync};
		#ifdef __DEBUG__
		_date = date;
		__TRACE_1("","_date");
		#endif
		GVAR(lastovercastchangetime) = time;
	} else {
		GVAR(lastovercast) = 0;
	};
	
	0 spawn {
		scriptName "spawn_overcast";
		waitUntil {sleep 0.123;!isNil QGVAR(overcast)};
		execFSM "fsms\WeatherClient.fsm";
	};
};

// TYRGHEN - ensures proper initialisation of the weather
if (GVAR(weather) > 0) then {
	0 setOvercast 0;
	0 spawn {
		scriptName "spawn_WeatherSetupScript";
		sleep 5;
		if !(isNil QGVAR(overcast)) then {
			0 setFog GVAR(overcast);
			sleep 5;
		};
		if !(isNil QGVAR(fog)) then {
			0 setFog GVAR(fog);
		};
	};
};

if (GVAR(FastTime) > 0) then {
	0 setOvercast 0;
	0 spawn {
		scriptName "spawn_TimeUpdateScript";
		waitUntil {sleep 0.221;!isNil {__XJIPGetVar(currentTime)}};
		while {true} do {
			sleep 1;
			skipTime (__XJIPGetVar(currentTime) - DayTime);
		};
	};
};

if (GVAR(IS_HC_CLIENT)) exitWith {
	__TRACE("Headless client found");
	__ccppfln(x_shc\x_setuphc.sqf);
};

__ccppfln(x_client\x_setupplayer.sqf);
